<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/27
 * Time: 14:43
 */
namespace app\admin\model;

class IntroduceUser extends DataBase
{
    protected $name = 'introduce_user';
    protected $autoWriteTimestamp = true;

    //推荐人流水
    public function getIntroduceDataList($page_config,$where)
    {
        $data =  $this
            ->alias('a')
            ->field('a.id,a.f_user_id,b.u_id,b.nickname,count(a.f_user_id) as count,b.introduce_money,a.create_time')
            ->join('users b','a.f_user_id = b.id','left')
            ->where($where)
            ->group('a.f_user_id')
            ->paginate($page_config);
        foreach($data as $key => $value){
            $where[] = ['user_id','=',$value['f_user_id']];
            $where[] = ['type','=',2];
            $where[] = ['status','=',3];
            $apply_introduce = Withdrawal::where($where)->sum('apply_amount');
            $data[$key]['all_introduce'] =$value['introduce_money'] + $apply_introduce;
            $data[$key]['apply_introduce'] = $apply_introduce;
            $data[$key]['remain_introduce'] = $value['introduce_money'];
        }
        return $data;
    }

    /**
     * Notes: 推荐人流水数据
     * User: 王俊成
     * Date: 2018/11/3
     * Time: 17:52
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function introduceFlowingData(){
        $page_config = array(
            'page' => input('pageNum',1),
            'row_lists' => input('limit',1)
        );
        $startTime = input('get.start_time');
        $endTime = input('get.end_time');
        $keyWords = input('get.keyWords');
        $map = [];
        if(!empty($keyWords) || $keyWords == '0'){
            $key = input('get.key');
            if($key == 'u_id'){
                $f_user_id = User::where('u_id',$keyWords)->value('id');
                if($f_user_id)
                    $map[] = ['f_user_id','=',$f_user_id];
            }
            if($key == 'nickname'){
                $f_user_id = User::where('nickname','like','%'.$keyWords.'%')->column('id');
                if($f_user_id)
                    $map[] = ['f_user_id','in',$f_user_id];
            }
        }
        if(!empty($startTime) && !empty($endTime)){
            $startTime = strtotime($startTime);
            $endTime = strtotime($endTime);
            if($startTime <= $endTime)
                $map[] = ['create_time','between',[$startTime,$endTime]];
            else
                $map[] = ['create_time','between',[$endTime,$startTime]];
        }
        else if(!empty($startTime)){
            $startTime = strtotime($startTime);
            $map[] = ['create_time','>=',$startTime];
        }
        else if(!empty($endTime)){
            $endTime = strtotime($endTime);
            $map[] = ['create_time','<=',$endTime];
        }

        $data = $this->where($map)->fieldRaw('f_user_id,user_id,COUNT(user_id) as num')->group('f_user_id')
            ->order('num DESC')->paginate($page_config);
        if($data){
            foreach($data as $key => $value){
                $value['u_id'] = get_uid($value['f_user_id']);
                $value['nickname'] = get_user_name($value['f_user_id']);
                $user_introduce = User::where('id',$value['f_user_id'])->value('introduce_money');
                $apply_introduce = Withdrawal::where([['user_id','=',$value['f_user_id']],['type','=',2],['status','=',3]])->sum('apply_amount');
                $value['all_money'] = $user_introduce + $apply_introduce;
                if($user_introduce)
                    $value['remain_money'] = $user_introduce;
                else
                    $value['remain_money'] = 0;
                $value['withdrawal_money'] = $apply_introduce;
                $data[$key] = $value;
            }
        }
        return $data;
    }
    /**
     * Notes: 被推荐人员信息
     * User: 王俊成
     * Date: 2018/11/5
     * Time: 11:55
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function introduceUserInfo(){
        $f_user_id = input('get.f_user_id');
        $user_id = $this->where('f_user_id',$f_user_id)->column('user_id');
        $pageConfig = [
            'list_rows' => 25,
            'page' => input('get.page',1),
            'query' =>[
                'f_user_id' => $f_user_id
            ]
        ];
        $data = User::where('id','in',$user_id)->field('id,u_id,nickname,u_phone,last_login')->paginate($pageConfig);
        if($data){
            foreach($data as $key => $value){
                $value['follow_num'] = SatinFollow::where('user_id',$value['id'])->count();
                $value['fans_num'] = SatinFollow::where('follow_user_id',$value['id'])->count();
                $value['recharge_money'] = Recharge::where([['user_id','=',$value['id']],['status','=',2]])->sum('amount');
            }
        }
        return $data;
    }

    /**
     * @title推荐人信息
     * @user shuaisuhai wu
     */
    public function getLastLoginAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }


    public function getUserInfo()
    {
        $map = [
            'b.id'=>'b.user_id',
            'b.u_id'=>'b.u_number'
        ];
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        //$order = input('sort','asort').' '.input('order','desc');
        $where = [];
        if(!empty(input('key')) && !empty(input('value'))){
            $key = input('key');
            $value = input('value');
            if($key=='u_id'){
                //$where[] = ['b.'.$key,'=',$value];
                $where[] = ['b.u_number','=',$value];
            }
            if($key=='nickname'){
                $where[] = ['b.'.$key,'like','%'.$value.'%'];
            }

        }

        return $this
            ->alias('a')
            ->field('a.f_user_id,b.u_number as u_id,b.nickname,b.last_login,count(a.user_id) as total_user')
            ->join('users b','a.f_user_id = b.user_id','left')
            ->where($where)
            ->group('a.f_user_id')
            ->paginate($page_config);

    }

    public function introduceMember()
    {
        $f_user_id = input('get.f_user_id');

        $begin_time = strtotime('2018-11-28 20:00:00');
        $userIds = $this->field('user_id')->where('f_user_id','=',$f_user_id)->where('created_at','>=',$begin_time)->select();


        $returnData = [];  //2018-11-28以后邀请的人
        $userModel = new User();
        $count =0;  //记录电话人数
        $total_user=0; //记录总人数
//        return $returnData;
//        halt('test');
        foreach($userIds as $k => $v){
            $returnData[$k] = $userModel->field('u_number as u_id,nickname,u_phone,oauth,last_login,money,gold,diamonds,source_register,source_login')->where('user_id',$v['user_id'])->find();

            if (!empty($returnData[$k]['u_phone'])) {
                $count += 1;
            }

            if(empty($returnData[$k])){
                unset($returnData[$k]);
            }else {
                $returnData[$k]['oauth'] = $returnData[$k]['oauth'] ? '微信登陆' : 'QQ登陆';
                $returnData[$k]['source_register'] = empty($returnData[$k]['source_register']) ? '无' : $returnData[$k]['source_register'];
                $returnData[$k]['source_login'] = empty($returnData[$k]['source_login']) ? '无' : $returnData[$k]['source_login'];
                $total_user += 1;
            }
        }
        $returnData['u_count'] = $count;  //记录电话用户总数
        $returnData['total_user'] = $total_user;  //记录总数
        $returnData['f_user_id'] = $f_user_id;  //传递参数
        if($total_user != 0){
            $returnData['ratio'] = round($count/$total_user, 2);//用户邀请有效率
        }else{
            $returnData['ratio'] = '';
        }  //分母不能为零
        return $returnData;
    }


    public function introduceAllMember()
    {
        $f_user_id = input('get.f_user_id');
        $userIds = $this->field('user_id')->where('f_user_id','=',$f_user_id)->select();
        $returnData = [];  //所有邀请的人
        $userModel = new User();


        foreach($userIds as $k => $v){
            $returnData[$k] = $userModel->field('u_id,nickname,u_phone,oauth,last_login,money,gold,diamonds,source_register,source_login')->where('id',$v['user_id'])->find();
            if(empty($returnData[$k])){
                unset($returnData[$k]);
            }else {
                $returnData[$k]['oauth'] = $returnData[$k]['oauth'] ? '微信登陆' : 'QQ登陆';
                $returnData[$k]['source_register'] = empty($returnData[$k]['source_register']) ? '无' : $returnData[$k]['source_register'];
                $returnData[$k]['source_login'] = empty($returnData[$k]['source_login']) ? '无' : $returnData[$k]['source_login'];
            }
        }

        return $returnData;
    }
}